//
//  WTStatusRetweetView.swift
//  WTSinaWeibo
//
//  Created by  Wellim on 16/6/29.
//  Copyright © 2016年  Wellim. All rights reserved.
//

import UIKit
import SnapKit

class WTStatusRetweetView: UIView {
// 声明一个全局的转发微博底部约束
    var retweetViewBottomConstraint:Constraint?
    
     // 供外界给其赋值
    var status: WTStatus?{
    
        didSet{
        
            cententLabel.attributedText = status?.retweeted_status?.textAttr
            
            /*
             - 卸载当前转发微博底部约束
             - 如果status?.retweeted_status?.pic_urls.count > 0 代表有配图
             - 显示配图
             - 赋值
             - 更新约束
             - 反之没有配图
             - 隐藏配图
             - 更新约束
             */
            // 卸载约束
            retweetViewBottomConstraint?.uninstall()
            
            // 有配图
            if status?.retweeted_status?.pic_urls?.count > 0 {
                //显示
                pictureView.hidden = false
              //赋值
                pictureView.pic_urls = status?.retweeted_status?.pic_urls
                // 更新约束
                self.snp_makeConstraints { (make) in
                    
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(StautsMargin).constraint
                }
 
                
            }else{
                // 没有配图
                // 隐藏配图
                 pictureView.hidden = true
                // 更新约束
                self.snp_makeConstraints { (make) in
                    
                    retweetViewBottomConstraint = make.bottom.equalTo(cententLabel).offset(StautsMargin).constraint
                }

            }
            
            
            

        }
    
    
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    
    private func setUpUI(){
    
        backgroundColor = RGB(237, green: 237, blue: 237)
        
        // 01- 添加控件
        addSubview(cententLabel)
        addSubview(pictureView)
        // 02- 添加约束
        cententLabel.snp_makeConstraints { (make) in
         
            make.left.equalTo(self).offset(StautsMargin)
            make.top.equalTo(self).offset(StautsMargin)
            
        }
        //配图
        pictureView.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 100, height: 100))
            make.left.equalTo(cententLabel)
            make.top.equalTo(cententLabel.snp_bottom).offset(StautsMargin)
            
        }
        // 给自己添加约束
        self.snp_makeConstraints { (make) in
            
        retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(StautsMargin).constraint
        }
        
//        cententLabel.text = "Wellim很吊Wellim很吊Wellim很吊Wellim很吊Wellim很吊Wellim很吊Wellim很吊Wellim很吊Wellim很吊Wellim很吊Wellim很吊Wellim很吊Wellim很吊Wellim很吊Wellim很吊Wellim很吊Wellim很吊"
        
    
    
    
    }
    
    
   // MARK: - 懒加载控件
    
    private lazy var cententLabel:UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15, maxWidth: kScreenWidth - 2 * StautsMargin)
    
    // 配图
    private lazy var pictureView:WTPictureView = WTPictureView()
    
    
    
}
